/**单链表节点 */
export class ListNode {
    /**值 */
    val: number
    /**下一个节点 */
    next: ListNode | null
    constructor(val?: number, next?: ListNode | null) {
        this.val = (val === undefined ? 0 : val)
        this.next = (next === undefined ? null : next)
    }
}
/**数组转单链表 */
export function arrToList<T extends number[]>(arr: T): T extends never[] ? null : T['length'] extends 0 ? null : ListNode {
    if (!arr || arr.length === 0) return null!
    const head: ListNode = new ListNode(0, null)
    let preNode = head
    for (let i = 0; i < arr.length; i++) {
        const node = new ListNode(arr[i], null)
        preNode.next = node
        preNode = node
    }
    return head.next as any
}
/**链表转数组 */
export function listToArr(node: ListNode | null): number[] {
    const res: number[] = []
    while (node) {
        res.push(node.val)
        node = node.next
    }
    return res
}